package com.fotkidemo;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.widget.Toast;
import com.fotkidemo.beans.Image;
import com.fotkidemo.tasks.LoadTask;
import com.fotkidemo.tasks.OnTaskCompleteListener;
import com.fotkidemo.tasks.TaskManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: roma
 * Date: 6/26/12
 * Time: 1:54 AM
 * To change this template use File | Settings | File Templates.
 */
public class ImagesLoadActivity extends FragmentActivity implements ImagesFragment.OnImagesClickListener, OnTaskCompleteListener {
    private static final String TAG = "ImagesFragment";
    public static final String EXTRA_URL = "extra_url";
    public static final String ALBUM_DIR = "album_dir";
    public static final String ALBUM_CACHE = "album_cache";

    private TaskManager taskManager;
    private String albumDir = "";


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        taskManager = new TaskManager(this, this);

        final String url = getIntent().getStringExtra(EXTRA_URL);
        albumDir = getIntent().getStringExtra(ALBUM_DIR);
        final String album_cache = getIntent().getStringExtra(ALBUM_CACHE);
        if (url != null) {
            if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
                final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.add(android.R.id.content, ImagesFragment.newInstance(url, album_cache), TAG);
                ft.commit();
            }
        }
    }

    @Override
    public void onImageClick(Image image, int position) {
        List<Image> imageList = new ArrayList<Image>();
        imageList.add(image);
        if (BuildConfig.DEBUG)
            Log.d(TAG, "start download images");

        taskManager.setupTask(new LoadTask(albumDir), imageList);
    }

    @Override
    public void onTaskComplete(LoadTask task) {
        if (task.isCancelled()) {
            Toast.makeText(this, R.string.task_canceled, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, R.string.task_completed, Toast.LENGTH_LONG).show();
        }
    }
}